package com.ly.datacenter.pojo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * function：
 * 
 * @author watson email: watson.wu@hotmail.com
 * @date 2009-9-4
 */
public class KeyValue {

	private String key;
	private Object value;

	public KeyValue(String key, Object value) {
		this.key = key;
		this.value = value;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	@Override
	public String toString() {
		return new StringBuffer(key).append(":").append(value).toString();
	}

	/**
	 * 
	 * function:
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public KeyValue[] toKeyValue(Map map) {
		KeyValue[] keyvalues = new KeyValue[map.size()];
		if(!map.isEmpty()) {
			Iterator iterator = map.keySet().iterator();
			int i=0;
			while(iterator.hasNext()) {
				String key = (String) iterator.next();
				keyvalues[i]=new KeyValue(key, map.get(key));
			}
		}
		return keyvalues;
	}

	/**
	 * 
	 * function:
	 * @param list
	 * @param key
	 * @return
	 */
	public static String getValueOf(List<KeyValue> list, String key) {
		if(null!=list){
			for(KeyValue keyvalue:list)
				if(keyvalue.getKey().equalsIgnoreCase(key))
					return keyvalue.getValue().toString();
		}
		return key;
	}

	/**
	 * 
	 * function:
	 * @param map
	 * @return
	 */
	public static List<Object> values(List<KeyValue> map) {
		List<Object> values = new ArrayList<Object>();
		for(KeyValue keyvalue:map){
			values.add(keyvalue.getValue());
		}
		return values;
	}
}
